package Sort.面试.工银澳门JAVA面试题;

import org.junit.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

public class BigDecimalApiTest {

    public static final BigDecimal ONE = new BigDecimal(1);
    public static final BigDecimal TWO = new BigDecimal(2);
    public static final BigDecimal THREE = new BigDecimal(3);
    public static final BigDecimal FOUR = new BigDecimal(4);
    public static final BigDecimal FIVE = new BigDecimal(5);
    public static final BigDecimal SIX = new BigDecimal(6);
    public static final BigDecimal SEVEN = new BigDecimal(7);
    public static final BigDecimal EIGHT = new BigDecimal(8);
    public static final BigDecimal NINE = new BigDecimal(9);
    public static final BigDecimal TEN = new BigDecimal(10);
    public static final BigDecimal ELEVEN = BigDecimal.valueOf(11);
    public static final BigDecimal TWELVE = BigDecimal.valueOf(12);
    public static final BigDecimal PI = BigDecimal.valueOf(3.14);
    public static final BigDecimal PI_EXACT = BigDecimal.valueOf(3.1415926); // exact->准确的

    @Test
    public void test(){
        // 1. multiply: 乘法
        BigDecimal num = ONE.multiply(TWELVE);
        System.out.println("乘法：multiply = " + num);

        // 2. divide: 除法
        num = num.divide(TWO);
        System.out.println("除法：divide = " + num);

        // 3. add: 加法
        num = num.add(FOUR);
        System.out.println("加法：add = " + num);

        // 4. subtract: 减法
        num = num.subtract(TWELVE);
        System.out.println("减法：subtract = " + num);
        System.out.println("--------------------------------------------");
        // 5. abs: 绝对值
        num = num.abs();
        System.out.println("绝对值：abs() = " + num);


        // 6. compareTo()
        int smallCompareBig = ONE.compareTo(TWO);
        int BigCompareSmall = TEN.compareTo(THREE);
        System.out.println("------ 比较：CompareTo(BigDecimal val) --------- ");
        System.out.println("smallCompareBig = " + smallCompareBig);
        System.out.println("BigCompareSmall = " + BigCompareSmall);

        // 7. pow(int num);
        System.out.println("--------- 绝对值：pow(int n) ----------------");
        num = FOUR.pow(5);
        System.out.println("pow(int n) = " + num);

        System.out.println("----------------- 取这个数值的精度：round -----------------------");
        num = PI.round(MathContext.DECIMAL64);
        System.out.println("round(MathContext.DECIMAL32) = " + num);

        System.out.println("-------------- 整数取精度：intValueExact() ------------------");
        System.out.println(BigDecimal.valueOf(3).intValueExact());

        System.out.println("-------------- 小数取四舍五入的精度：intValue() ------------------");
        System.out.println(BigDecimal.valueOf(3.1415926).intValue());

        System.out.println("----------- movePointLeft/movePointRight(int n)  小数点 左/右 移n位----------");
        System.out.println("PI_EXACT.movePointLeft(3) = " + PI_EXACT.movePointLeft(3));
        System.out.println("PI.movePointRight(2) = " + PI.movePointRight(2));
    }
}
